package com.lanren.hovertitlerecylerview;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;


/**
 * @ClassName HoverTitleItemDecoration
 * @Description TODO HoverTitle对应的ItemDecoration
 * @Author chongheng.wang
 * @Date 2019/8/9 14:23
 * @Version 1.0
 */
public class HoverTitleItemDecoration extends RecyclerView.ItemDecoration implements IHoverTitleDecoration {

    private Rect mHoverTitleRect = null;
    private int mHoverTitlePosition = -1;
    private boolean isDrawTop = true;//是否将item悬浮显示

    /**
     * 把要固定的View绘制在上层
     */
    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        //确保是PinnedHeaderAdapter的adapter,确保有View
        if (parent.getAdapter() instanceof HoverTitleAdapter && parent.getChildCount() > 0) {
            HoverTitleAdapter adapter = (HoverTitleAdapter) parent.getAdapter();
            //找到要固定的pin view
            View firstView = parent.getChildAt(0);
            int firstAdapterPosition = parent.getChildAdapterPosition(firstView);
            int hoverTitlePosition = getHoverTitleViewPosition(firstAdapterPosition, adapter);
            mHoverTitlePosition = hoverTitlePosition;
            if (hoverTitlePosition != -1) {
                RecyclerView.ViewHolder hoverTitleViewHolder = adapter.onCreateViewHolder(parent, adapter.getItemViewType
                        (hoverTitlePosition));
                adapter.onBindViewHolder(hoverTitleViewHolder, hoverTitlePosition);
                //要固定的view
                View hoverTitleView = hoverTitleViewHolder.itemView;
                ensureHoverTitleViewLayout(hoverTitleView, parent);
                int sectionPinOffset = 0;
                for (int index = 0; index < parent.getChildCount(); index++) {
                    if (adapter.isHoverPosition(parent.getChildAdapterPosition(parent.getChildAt(index)))) {
                        View sectionView = parent.getChildAt(index);
                        int sectionTop = sectionView.getTop();
                        int pinViewHeight = hoverTitleView.getHeight();
                        if (sectionTop < pinViewHeight && sectionTop > 0) {
                            sectionPinOffset = sectionTop - pinViewHeight;
                        }
                    }
                }
                int saveCount = c.save();
                RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) hoverTitleView.getLayoutParams();
                if (layoutParams == null) {
                    throw new NullPointerException("HoverTitleItemDecoration");
                }
                c.translate(layoutParams.leftMargin, sectionPinOffset);
                c.clipRect(0, 0, parent.getWidth(), hoverTitleView.getMeasuredHeight());
                if (isDrawTop) {
                    hoverTitleView.draw(c);
                }
                c.restoreToCount(saveCount);
                if (mHoverTitleRect == null) {
                    mHoverTitleRect = new Rect();
                }
                mHoverTitleRect.set(0, 0, parent.getWidth(), hoverTitleView.getMeasuredHeight() + sectionPinOffset);
            } else {
                mHoverTitleRect = null;
            }

        }
    }

    /**
     * 要给每个item设置间距主要靠这个函数来实现
     */
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    }

    /**
     * 根据第一个可见的adapter的位置去获取临近的一个要固定的position的位置
     *
     * @param adapterFirstVisible 第一个可见的adapter的位置
     * @return -1：未找到 >=0 找到位置
     */
    private int getHoverTitleViewPosition(int adapterFirstVisible, HoverTitleAdapter adapter) {
        for (int index = adapterFirstVisible; index >= 0; index--) {
            if (adapter.isHoverPosition(index)) {
                return index;
            }
        }
        return -1;
    }

    private void ensureHoverTitleViewLayout(View pinView, RecyclerView recyclerView) {
        if (pinView.isLayoutRequested()) {
            /**
             * 用的是RecyclerView的宽度测量，和RecyclerView的宽度一样
             */
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) pinView.getLayoutParams();
            if (layoutParams == null) {
                throw new NullPointerException("HoverTitleItemDecoration");
            }
            int widthSpec = View.MeasureSpec.makeMeasureSpec(
                    recyclerView.getMeasuredWidth() - layoutParams.leftMargin - layoutParams.rightMargin, View.MeasureSpec.EXACTLY);

            int heightSpec;
            if (layoutParams.height > 0) {
                heightSpec = View.MeasureSpec.makeMeasureSpec(layoutParams.height, View.MeasureSpec.EXACTLY);
            } else {
                heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            }
            pinView.measure(widthSpec, heightSpec);
            pinView.layout(0, 0, pinView.getMeasuredWidth(), pinView.getMeasuredHeight());
        }
    }

    @Override
    public Rect getHoverTitleRect() {
        return mHoverTitleRect;
    }

    @Override
    public int getHoverTitlePosition() {
        return mHoverTitlePosition;
    }

    @Override
    public void setDrawTop(boolean isTop) {
        this.isDrawTop = isTop;
    }


    @Override
    public boolean getDrawTop() {
        return isDrawTop;
    }

}
